package com.limh.readers.activity

import android.support.v4.view.ViewPager
import android.view.KeyEvent
import android.widget.RadioGroup
import butterknife.BindView
import com.limh.readers.R
import com.limh.readers.adapter.FragmentAdapter
import com.limh.readers.fragment.*
import android.widget.Toast


class MainActivity : BaseActivity() {
    @BindView(R.id.vp_main)
    lateinit var vpMain: ViewPager
    @BindView(R.id.radio_menu)
    lateinit var radioMenu: RadioGroup

    override fun findView() {
        setContentView(R.layout.activity_main)
    }

    override fun initViews() {
        val fragments: ArrayList<BaseFragment> = ArrayList()
        fragments.add(HomeFragment())
        fragments.add(PastFragment())
        fragments.add(NoticeFragment())
        fragments.add(MineFragment())
        vpMain.adapter = FragmentAdapter(supportFragmentManager, fragments)
        vpMain.currentItem = 0
        vpMain.offscreenPageLimit = 3
        radioMenu.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.nav_bottom_menu_1 -> vpMain.setCurrentItem(0, true)
                R.id.nav_bottom_menu_2 -> vpMain.setCurrentItem(1, true)
                R.id.nav_bottom_menu_3 -> vpMain.setCurrentItem(2, true)
                R.id.nav_bottom_menu_4 -> vpMain.setCurrentItem(3, true)
            }
        }
    }

    override fun initDatas() {
    }

    private var exitTime: Long = 0
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.makeText(applicationContext, "再按一次退出程序",
                        Toast.LENGTH_SHORT).show()
                exitTime = System.currentTimeMillis()
            } else {
                android.os.Process.killProcess(android.os.Process.myPid())   //获取PID
                System.exit(0)
                finish()
            }
        }
        return true
    }
}

